package com.jaay.service;

import com.jaay.common.CommonResult;
import com.jaay.common.RandomUtils;
import com.jaay.dao.OrderDao;
import com.jaay.pojo.Order;
import com.jaay.pojo.Product;
import com.jaay.repository.OrderRepository;
import com.jaay.repository.ProductRepository;
import com.jaay.snowflake.SnowflakeGenerator;
import dto.OrderDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;

/**
 * @author chencanfeng
 */
@Transactional
@Service
@Slf4j
public class OrderService extends BaseService<Order, Long> {

    @Autowired
    private SnowflakeGenerator snowflakeGenerator;
    @Autowired
    private RandomUtils randomUtils;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductRepository productRepository;

    public OrderService(OrderRepository orderRepository) {
        super(orderRepository);
    }

    /**
     * 生成订单
     * @param orderList 订单列表
     * @return 功结果
     */
    public CommonResult<String> batchAddOrder(List<OrderDto> orderList) {

        try {

            List<Product> productList = new ArrayList<>();

            List<Order> orders = orderList.stream().map(orderDto -> {
                        Optional<Order> optional = orderDto.copy();
                        Order order = optional.orElse(new Order());
                        order.setId(snowflakeGenerator.nextId());
                        order.setOrderNo(randomUtils.generateOrderNo());
                        order.setOrderDate(Calendar.getInstance().getTime());
                        order.setMerchantNo(randomUtils.generateMerchantNo());

                        productList.addAll(orderDto.getProductList().stream()
                                .map(productDto -> {

                                    Optional<Product> productOptional = productDto.copy();
                                    Product product = productOptional.orElse(new Product());
                                    product.setId(snowflakeGenerator.nextId());
                                    product.setOrderNo(order.getOrderNo());

                                    return product;
                                }).toList());

                        return order;
                    }
            ).toList();

            productRepository.saveAll(productList);
            orderRepository.saveAll(orders);


            return new CommonResult<String>().success();

        } catch (Exception e) {

            log.error("add order error", e);

            return new CommonResult<String>().fail();
        }
    }


    public CommonResult<List<Order>> queryOrderAndProductList() {

        return new CommonResult<List<Order>>().success(orderDao.queryOrderAndProductList());

    }

}
